### -*- coding: utf-8 -*- #############################################
# Разработано Паэглит Арвидом (http://arvid.dp.ua/)
# Все права защищены, 2007
#
# Developed by Paeglit Arvid (http://arvid.dp.ua/)
# All right reserved, 2007
#######################################################################
"""Selection class for the Zope 3 based diplom package

$Id: selection.py 66 2007-12-28 23:05:19Z Arvi3D $
"""
__author__  = "Arvid"
__license__ = "GPL"
__version__ = "$Revision: 66 $"
__date__ = "$Date: 2007-12-28 23:05:19 +0000 (Fri, 28 Dec 2007) $"

from zope.schema.fieldproperty import FieldProperty
from zope.interface import implements,implementedBy
from zope.cachedescriptors.property import CachedProperty
from persistent import Persistent
from zope.app.container.contained import Contained

from math import sqrt, exp

from interfaces import ISelection

class Selection(Persistent, Contained) :
    implements(ISelection)

    dimension = FieldProperty(ISelection['dimension'])
    vector = ()

    def __init__(self):
        super(Selection, self).__init__()

    def nu(self, k):
        """get begin moment"""
        return sum(map(lambda v : v**k, self.vector))/self.dimension

    def mu(self, k):
        """get central moment"""
        nu1 = self.nu(1)
        return sum(map(lambda v : (v - nu1)**k, self.vector))/self.dimension

    @property
    def mean(self):
        """avarage"""
        return self.nu(1)

    def var(self, zsuneniy = True):
        """get sqr of square distance"""
        if zsuneniy:
            return self.mu(2)
        else:
            return self.dimension/(self.dimension-1)*self.mu(2)

    def sigma(self, zsuneniy = True):
        """get square distance"""
        return sqrt(self.var(zsuneniy))

    def eksces(self, zsuneniy = True):
        """ get eksces"""
        E = self.mu(4)/(self.var()**2)
        if zsuneniy:
            return E
        else:
            n = self.dimension
            E = (n*n-1.0)/(n-2.0)/(n-3.0)*(6.0/(n+1) + (E-3))
            return E

